package com.hj.config.security.handler;

import com.hj.domain.LoginUser;
import com.hj.response.Resp;
import com.hj.response.RespInfo;
import com.hj.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 认证登录成功处理
 * @author hj
 */
@Slf4j
@Component
public class AuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Resource
    private LoginUserHandler loginUserHandler;


    /**
     * 登录成功被调用
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) {
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        loginUserHandler.packingUser(loginUser);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        ResponseUtil.responseJson(response, new Resp<>(RespInfo.SUCCESS, loginUser));
    }


}
